package com.example.bilibili.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.bilibili.domain.*;
import com.example.bilibili.mapper.BiliUserMapper;
import com.example.bilibili.mapper.GuanzhuMapper;
import com.example.bilibili.service.*;
import com.example.bilibili.util.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/user")
public class QTUserController {
    @Autowired
    private BiliUserService biliUserService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private BiliUserMapper biliUserMapper;
    @Autowired
    private GuanzhuService guanzhuService;
    @Autowired
    private BiliVideoService biliVideoService;
    @Autowired
    private GuanzhuMapper guanzhuMapper;
    @Autowired
    private DianzanService dianzanService;
    @RequestMapping("/login")
    public Object login(String username,String password) throws NoSuchAlgorithmException {
        MD5 md5=new MD5();
        password=md5.EncoderPwdByMD5(password);
        QueryWrapper qw= new QueryWrapper();
        qw.select();
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("userName", username);
        paramsMap.put("password", password);
        qw.allEq(paramsMap);
        BiliUser user= biliUserService.getOne(qw);
        if(user==null)
        {
            return  0;
        }
        System.out.println(user.getUserid());
        return user.getUserid();
    }
    //用户注册
    @RequestMapping("/userRegister")
    public int userRegister(String username,String password,String userauthor) throws NoSuchAlgorithmException {
        MD5 md5=new MD5();
        password=md5.EncoderPwdByMD5(password);
        QueryWrapper<BiliUser> qw1= new QueryWrapper<>();
        qw1.eq("username",username);
        BiliUser bu1=biliUserService.getOne(qw1);
        if(bu1!=null)
            return 1;//该用户名已存在

        if(username.length()<=2||username.length()>=8)
            return 2;//用户名长度非法

        String IllegalString="^(陈睿|非法名称1|非法名称2)$";

        //判断用户名合法性
        Pattern p = Pattern.compile(IllegalString);
        Matcher m = p.matcher(username);
        Boolean mM=m.matches();
        if(mM)
            return 3;//用户名非法

        //判断是否已经存在该昵称
        QueryWrapper<BiliUser> qw2= new QueryWrapper<>();
        qw2.eq("userauthor",userauthor);
        BiliUser bu2=biliUserService.getOne(qw2);
        if(bu2!=null)
            return 4;//已存在该昵称

        //判断昵称合法性
        Pattern p2 = Pattern.compile(IllegalString);
        Matcher m2 = p2.matcher(userauthor);
        Boolean mM2=m2.matches();
        if(mM2)
            return 5;//昵称非法

        //判断密码长度合法性
        if(password.length()<=3||password.length()>=50)
            return 6;

        //向User表插入
        BiliUser bu=new BiliUser();
        bu.setUsername(username);
        bu.setPassword(password);
        bu.setUserauthor(userauthor);
        bu.setUserrole(1);
        bu.setUseravatar("https://upload1314.oss-cn-qingdao.aliyuncs.com/userimg/moren.png");
        return biliUserService.save(bu)?7:8;//注册成功
    }
    @RequestMapping("/test")
    public Object test(){
        return "test!";
    }

    //提交评论
    @RequestMapping("/getcommen")
    public Object getCommen(Long userid,String videoid,String content){
        Comment cm=new Comment();
        cm.setUserid(userid);
        cm.setVideoid(videoid);
        cm.setContent(content);
        return commentService.save(cm)?1:0;
    }
    @RequestMapping("/getuserstate")
    public Object getuserstate(Long userid){

        uservo user=biliUserMapper.getuserstate(userid);
        return JSON.toJSONString(user);
    }
    @RequestMapping("/getuserauthor")
    public Object getuserauthor(Long userid){

        uservo user=biliUserMapper.getuserstate(userid);
        return user.getUserauthor();
    }
    ////////////////////////////////////////
               //  关注
    ////////////////////////////////////
    @RequestMapping("/checkguanzhu")
    public Object checkguanzhu(Long userid,String videoid){
        QueryWrapper qw0= new QueryWrapper();
        qw0.select("uploaderId");
        qw0.eq("videoId",videoid);
        BiliVideo biliVideo=biliVideoService.getOne(qw0);
        long uploaderid=biliVideo.getUploaderid();

        QueryWrapper qw= new QueryWrapper();
       qw.eq("userId",userid);
        qw.eq("uploaderId",uploaderid);
        Guanzhu guanzhu=guanzhuService.getOne(qw);
        if(guanzhu==null)
            return 0;
        return 1;
    }

    @RequestMapping("/guanzhu")
    public Object guanzhu(Long userid,String videoid){
        QueryWrapper qw0= new QueryWrapper();
        qw0.select("uploaderId");
        qw0.eq("videoId",videoid);
        BiliVideo biliVideo=biliVideoService.getOne(qw0);
        long uploaderid=biliVideo.getUploaderid();

        Guanzhu guanzhu=new Guanzhu();
        guanzhu.setUserid(userid);
        guanzhu.setUploaderid(uploaderid);
        return guanzhuService.save(guanzhu)?1:0;
    }
    @RequestMapping("/quguan")
    public Object quguan(Long userid,String videoid){
        QueryWrapper qw0= new QueryWrapper();
        qw0.select("uploaderId");
        qw0.eq("videoId",videoid);
        BiliVideo biliVideo=biliVideoService.getOne(qw0);
        long uploaderid=biliVideo.getUploaderid();

        QueryWrapper qw= new QueryWrapper();
        qw.eq("uploaderId",uploaderid);
        qw.eq("userId",userid);

        return guanzhuService.remove(qw)?1:0;
    }

    @RequestMapping("/getguanzhu")
    public Object getguanzhu(Long userid){
     List<guanzhuvo> list=guanzhuMapper.getguanzhu(userid);

        return JSON.toJSONString(list);
    }

    @RequestMapping("/listquguan")
    public Object listquguan(Long userid,Long guanzhuid){

        QueryWrapper qw= new QueryWrapper();
        qw.eq("uploaderId",guanzhuid);
        qw.eq("userId",userid);

        return guanzhuService.remove(qw)?1:0;
    }
    ////////////////////////////////////////
    //  点赞
    ////////////////////////////////////
    @RequestMapping("/checkdianzan")
    public Object checkdianzan(Long userid,String videoid){
        QueryWrapper qw= new QueryWrapper();
        qw.eq("userId",userid);
        qw.eq("videoId",videoid);
        Dianzan dianzan=dianzanService.getOne(qw);
        if(dianzan==null)
            return 0;
        return 1;
    }
    @RequestMapping("/dianzan")
    public Object dianzan(Long userid,String videoid){

        Dianzan dianzan=new Dianzan();
        dianzan.setUserid(userid);
        dianzan.setVideoid(videoid);
        dianzanService.save(dianzan);

        QueryWrapper qw= new QueryWrapper();
        qw.eq("videoId",videoid);
        BiliVideo biliVideo=biliVideoService.getOne(qw);
        biliVideo.setThumbs(biliVideo.getThumbs()+1);

        return biliVideoService.update(biliVideo,qw);
    }
    @RequestMapping("/qudianzan")
    public Object qudianzan(Long userid,String videoid){
        QueryWrapper qw0= new QueryWrapper();
        qw0.eq("userId",userid);
        qw0.eq("videoId",videoid);
        dianzanService.remove(qw0);


        QueryWrapper qw= new QueryWrapper();
        qw.eq("videoId",videoid);
        BiliVideo biliVideo=biliVideoService.getOne(qw);
        biliVideo.setThumbs(biliVideo.getThumbs()-1);

        return biliVideoService.update(biliVideo,qw);
    }


}
